package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: Limingjie
 * @date: 2020/10/28-6:03 PM
 * @Version: 1.0.0
 */
@Service
@Slf4j
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public Menu editById(Integer id) {
        return menuDao.editById(id);
    }

    //判断菜单是否被角色关联
    @Override
    public void deleteById(Integer id) {
        long count = menuDao.findRelationById(id);
        if (count>0){
            //说明被关联，不能删除
            throw new BusinessRuntimeException("该菜单被角色表关联，不能删除");
        }else {
            menuDao.deleteById(id);
        }
    }

    @Override
    public PageResult findMenuPage(QueryPageBean queryPageBean) {
        log.debug("service:" + queryPageBean);
        //1. 开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2. 条件查询
        Page<Menu> page = menuDao.findByCondition(queryPageBean.getQueryString());
        log.debug("page{"+page+"}");
        //封装PageResult
        return new PageResult(page.getTotal(), page);
    }



    @Override
    public void add(Menu menu,Integer level) {
        menuDao.add(menu,level);
    }


    @Override
    public long findWhetherExists(String path) {
        return menuDao.findWhetherExists(path);
    }

    @Override
    public Integer findMaxPriority(Integer level) {
        return menuDao.findMaxPriority(level);
    }

    @Override
    public String findMaxPriorityByCondition(Integer level, String parentMenuId) {
        return menuDao.findMaxPriorityByCondition(level,parentMenuId);
    }

    @Override
    public Integer findParentId(String parentMenuId) {
        return menuDao.findParentId(parentMenuId);
    }

    @Override
    public List<Menu> findAll() {

        List<Menu> menuList=menuDao.findAll();

        return menuList;
    }

    @Override
    public Result getMenuList(String username) {
        //先根据当前登录的用户的username查询角色，返回一个角色集合
        List<Role> roleList = menuDao.findRolesByUsername(username);

        //前端需要展示的菜单列表集合
        List<Map<String,Object>> menuList = new ArrayList<>();

        /*Map<String, Object> need2AddMenu = new HashMap<>();*/

        //遍历角色集合
        for (Role role : roleList) {
            //根据角色id查询当前角色可以访问的菜单,一级菜单
            List<Map<String,Object>> numOneMenuList = menuDao.findNumOneMenuByRoleId(role.getId());
            //遍历一级菜单列表集合
            for (Map<String,Object> menu : numOneMenuList) {
                //根据menu表中的id字段查询level为2的子目录
                Integer id = (Integer) menu.get("id");
                List<Map<String,Object>> sonMenu = menuDao.findNumTwoMenuByRoleId(id);

                menu.put("children",sonMenu);
                menuList.add(menu);
            }
        }
        return new Result(true, "欢迎" + username,menuList);
    }

}
